package com.example.demo.customer;

//业务层

import com.example.demo.exception.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CustomerService {

//    声明Logger类型的变量（从slf4j库引入！）
    private final static Logger LOGGER = LoggerFactory.getLogger((CustomerService.class));

    private final CustomerRepository customerRepository;

    @Autowired
    public CustomerService(CustomerRepository customerRepository){
        this.customerRepository = customerRepository;
    }



    List<Customer> getCustomers() {
//        执行getCustomers()时输出日志，日志级别为info
        LOGGER.info("getCustomers called");
        return customerRepository.findAll();
    }

//    新建getCustomer()方法
    Customer getCustomer(Long id){
        return customerRepository
                .findById(id)
                .orElseThrow(() -> {
                    NotFoundException notFoundException = new NotFoundException("customer  " + id +" 无法找到");
                    LOGGER.error("error: getCustomer {} " + id , notFoundException);
                    return notFoundException;
                });


//        return getCustomers()
//                .stream()
//                .filter(customer -> Objects.equals(customer.getId(),id))
//                .findFirst()

    }

}
